package com.cgs.query.server.controller

import com.cgs.query.domain.Project2Define
import com.cgs.query.domain.QueryProject
import com.cgs.query.service.IProject2DefineService
import com.cgs.query.service.IProjectErrorLogService
import com.cgs.query.service.IProjectLockLogService
import com.cgs.query.service.IQueryProjectService
import io.swagger.annotations.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile

@RestController
@RequestMapping("/project")
@Api(description = "项目管理模块")
class QueryProjectController {

    @Autowired
    private lateinit var queryProjectService: IQueryProjectService
    @Autowired
    private lateinit var projectLockLogService: IProjectLockLogService
    @Autowired
    private lateinit var projectErrorLogService: IProjectErrorLogService
    @Autowired
    private lateinit var project2DefineService: IProject2DefineService

    @GetMapping("/{name}")
    @ApiOperation(value = "通过项目名称查询")
    fun findOne(@ApiParam(value = "编号", required = true) @PathVariable name: String) = queryProjectService.findByName(name)

    @GetMapping("/lockLog/{name}")
    @ApiOperation(value = "查询项目锁定日志")
    fun findLockLog(@ApiParam(value = "编号", required = true) @PathVariable name: String) = projectLockLogService.findByName(name)

    @GetMapping("/errorLog/{name}")
    @ApiOperation(value = "查询项目错误日志")
    fun findErrorLog(@ApiParam(value = "编号", required = true) @PathVariable name: String,
                     @ApiParam(value = "当前页数") @RequestParam(required = false, defaultValue = "0") pn: Int,
                     @ApiParam(value = "每页大小") @RequestParam(required = false, defaultValue = "15") ps: Int) =
            projectErrorLogService.findByProjectName(name, pn, ps)

    @PostMapping("/")
    @ApiOperation(value = "新建项目")
    fun insert(@ApiParam(value = "项目定义", required = true) @RequestBody project: QueryProject) = queryProjectService.insert(project)

    @PostMapping("/edit")
    @ApiOperation(value = "修改项目")
    fun edit(@ApiParam(value = "项目定义", required = true) @RequestBody project: QueryProject) = queryProjectService.save(project)

    @PostMapping("/define/")
    @ApiOperation(value = "项目中定义关联保存")
    fun project2DefineSave(@ApiParam(value = "项目定义", required = true) @RequestBody project2Define: Project2Define) = project2DefineService.save(project2Define)

    @DeleteMapping("/{name}")
    @ApiOperation(value = "通过项目名称删除")
    fun delete(@ApiParam(value = "编号", required = true) @PathVariable name: String) = queryProjectService.delete(name)

    @DeleteMapping("/errorLog/{name}")
    @ApiOperation(value = "通过项目名称删除错误日志")
    fun deleteErrorLog(@ApiParam(value = "项目名", required = true) @PathVariable name: String) = projectErrorLogService.deleteByProjectName(name)

    @GetMapping("/list")
    @ApiOperation(value = "展示项目列表")
    fun findAll() = queryProjectService.findAll()

    @GetMapping("/list/{projectName}")
    @ApiOperation(value = "展示项目定义列表")
    fun findProjectDefineVo(@ApiParam(value = "项目名称", required = false) @PathVariable(required = false) projectName: String) =
            queryProjectService.findProjectDefineVoByProjectName(projectName)

    @PostMapping("/import/define/{projectName}")
    @ApiOperation(value = "向项目中导入多个定义名")
    fun importDefines(@ApiParam(value = "项目名称", required = true) @PathVariable projectName: String,
                      @ApiParam(value = "定义编号数组", required = true) @RequestBody defines: List<String>) {
        queryProjectService.importDefines(projectName, defines)
    }

    @PostMapping("/import/tag/{projectName}")
    @ApiOperation(value = "向项目中导入一个标签")
    fun importTag(@ApiParam(value = "项目名称", required = true) @PathVariable projectName: String,
                  @ApiParam(value = "标签别名", required = true) @RequestParam alias: String,
                  @ApiParam(value = "标签名称", required = true) @RequestParam tagName: String) {
        queryProjectService.importTag(projectName, alias, tagName)
    }

    @GetMapping("/validate/{projectName}")
    @ApiOperation("检验项目中的重名情况")
    fun validate(@ApiParam(value = "项目名称", required = true) @PathVariable projectName: String) =
            queryProjectService.validate(projectName)

    @GetMapping("/lock/{projectName}")
    @ApiOperation("检查并锁定项目")
    fun lock(@ApiParam(value = "项目名称", required = true) @PathVariable projectName: String) =
            queryProjectService.lock(projectName)

    @GetMapping("/copy/{targetProjectName}/{originProjectName}")
    @ApiOperation("项目拷贝")
    fun copy(@ApiParam(value = "项目名称", required = true) @PathVariable targetProjectName: String,
             @ApiParam(value = "项目名称", required = true) @PathVariable originProjectName: String) =
            queryProjectService.copy(targetProjectName, originProjectName)

    @DeleteMapping("/clean/{projectName}")
    @ApiOperation("项目清空")
    fun cleanProject(@ApiParam(value = "项目名称", required = true) @PathVariable projectName: String) {
        queryProjectService.clean(projectName)
    }

    @DeleteMapping("/define/{id}")
    @ApiOperation("移除定义")
    fun removeDefine(@ApiParam(value = "定义id", required = true) @PathVariable id: Int) {
        project2DefineService.delete(id)
    }

    // 新增连接
    @PostMapping("/import/project")
    @ApiOperation(value = "上传.json文件")
    fun importProject(@ApiParam(required = true, value = "文件") @RequestParam(value = "file") file: MultipartFile) =
            queryProjectService.import(file)

    @GetMapping("/export/{projectName}")
    @ApiOperation(value = "导出项目")
    fun export(@ApiParam(value = "编号", required = true) @PathVariable projectName: String): ResponseEntity<String> =
            queryProjectService.export(projectName)

}